package com.tanhua.server.service;

import com.tan.model.domain.User;
import com.tan.model.domain.UserInfo;
import com.tan.model.domain.announcement;
import com.tan.model.enums.CommentType;
import com.tan.model.mongo.Comment;
import com.tan.model.mongo.Friend;
import com.tan.model.vo.*;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.server.interceptor.Userthread;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HuanxinService {
    @DubboReference
    private Userapi userApi;
    @DubboReference
    private UserInfoapi userInfoApi;
    @DubboReference
    private FriendApi friendApi;
    @DubboReference
    private CommentApi commentApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private AnnouncementApi announcementApi;
    public PageResult messageCount(Integer page, Integer pagesize,int type) {

        Long userId = Userthread.getUser().getId();
        List<Comment> likecomment = commentApi.getLikeCount(userId, type);
        List<LikeVo> likecomment1 = new ArrayList<>();
        for (Comment comment1 : likecomment) {
            UserInfo userInfo = userInfoApi.getUserInfo(comment1.getUserId());
            LikeVo likeVo = LikeVo.init(userInfo, comment1);
            likecomment1.add(likeVo);
        }
        PageResult pageResult = new PageResult(page,pagesize,likecomment1.size(),likecomment1);
        return pageResult;

    }
    public HuanXinUserVo searchUser() {
        Long userId = Userthread.getUser().getId();
     HuanXinUserVo huanXinUserVo =  userApi.searchUser(userId);


       return huanXinUserVo;

    }

    public UserInfoVo searchUserInfo(String huanxinUserId) {
        User user = userApi.searchUserInfo(huanxinUserId);
        UserInfoVo userInfoVo=new UserInfoVo();
        Long id = user.getId();
        UserInfo userInfo = userInfoApi.searchUserInfo(id);
        BeanUtils.copyProperties(userInfo,userInfoVo);
        if (userInfo.getAge()!=null){
            userInfoVo.setAge(userInfo.getAge().toString());
        }

        return userInfoVo;
    }

    public void addFriend(Long userId) {
        Long currentUserId = Userthread.getUser().getId();
        friendApi.saveFriend(currentUserId,userId);
        huanXinTemplate.addContact(Constants.HX_USER_PREFIX+currentUserId, Constants.HX_USER_PREFIX+userId);

    }

    public PageResult searchFriend(Integer page, Integer pagesize, String keyword) {
        Long userId = Userthread.getUser().getId();
         List<Friend> friends =   friendApi.searchFriends(userId);
         List<ContactVo> contactVos = new ArrayList<>();
         for (Friend friend : friends) {
             Long id = friend.getUserId();
           UserInfo userInfo =  userInfoApi.getUserInfokeyword(id,keyword);
           if (userInfo!=null){
               ContactVo contactVo = ContactVo.init(userInfo);
               contactVos.add(contactVo);
           }

         }
         PageResult pageResult = new PageResult(page,pagesize,contactVos.size(),contactVos);
         return pageResult;
    }

    public PageResult getLikeCount(Integer page, Integer pagesize) {
      PageResult pageResult = this.messageCount(page, pagesize, CommentType.LIKE.getType());
      return pageResult;
    }
    public PageResult getcommentCount(Integer page, Integer pagesize) {
      PageResult pageResult = this.messageCount(page, pagesize, CommentType.COMMENT.getType());
      return pageResult;
    }

    public PageResult getLoveCount(Integer page, Integer pagesize) {
        PageResult pageResult = this.messageCount(page, pagesize, CommentType.LOVE.getType());
        return pageResult;


    }





    public PageResult announcementsCount(Integer page, Integer pagesize) {
        List<announcement> announcements = announcementApi.getAnnouncements(page, pagesize);
        List<AnnouncementVo> announcementVos = new ArrayList<>();
        for (announcement announcement : announcements) {
           AnnouncementVo announcementVo = AnnouncementVo.init(announcement);
           announcementVos.add(announcementVo);
        }
        PageResult pageResult = new PageResult(page,pagesize,announcementVos.size(),announcementVos);
        return pageResult;
    }
}
